package com.logos.musicframe.config;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @author logos
 * date 2025/6/03 22:21
 * @version v1.0
 * @Package com.logos.musicframe.config
 */
@Configuration
@Slf4j
public class MinioConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${minio.url}")
    private String url;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    private MinioClient minioClient;

    @Bean
    public MinioClient getMinioClient() {
        refreshMinioClient();
        return minioClient;
    }

    private void refreshMinioClient() {
        if (url != null && accessKey != null && secretKey != null) {
            log.info("url:{},accessKey:{},secretKey:{}",url,accessKey,secretKey);
            this.minioClient = MinioClient.builder()
                    .endpoint(url)
                    .credentials(accessKey, secretKey)
                    .build();
        }
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        refreshMinioClient();
    }
}
